/**
 * {作业名称}
 *
 * 输入参数:
 *
 * 输出参数:
 *
 */
(() => {
  // 私有区域

  return function ({ UID, PageIds }) {
    // 验证输入参数
    if (!UID) throw Error("无效会话");

    const items = A.cache.get('__user_menus__.' + UID, () => {
      // 加载被直接授权的菜单项
      const { rows } = A.db.execute(A.card('B.用户').连接名称, "SELECT B.CD_ID FROM B_YHJS A, B_JSCD B WHERE A.JS_ID=B.JS_ID AND A.YH_ID=? UNION SELECT B.CD_ID FROM B_YHJG A, B_JGCD B WHERE A.JG_ID=B.JG_ID AND A.YH_ID=? UNION SELECT B.CD_ID FROM B_YHGW A, B_GWCD B WHERE A.GW_ID=B.GW_ID AND A.YH_ID=? UNION SELECT B.CD_ID FROM B_YHZW A, B_ZWCD B WHERE A.ZW_ID=B.ZW_ID AND A.YH_ID=?", [UID, UID, UID, UID]);
      const cd_ids = rows.map(r => r.CD_ID);

      // 加载授权菜单和所有子菜单
      const { Rows: items } = A.job.TreeR({
        UID,
        Card: '子系统菜单',
        Model: ['上级', '页面'],
        Filter: [['ID', 'in', cd_ids]],
        Parent: '上级',
      });
      return items;
    }, 60);

    // 按树结构遍历收集所有节点
    const ids = [];
    const traverse = (node) => {
      if (node.页面) ids.push(node.页面);
      if (node.children) node.children.forEach(traverse);
    };
    items.forEach(traverse);

    // 返回结果
    return PageIds.some(id => ids.includes(id));
  };
})()